class Student:
    """学生类，包含姓名、年龄和成绩"""
    def __init__(self, name, age, score):
        """初始化学生属性"""
        self.name = name
        self.age = age
        self.score = score

        def display_info(self):
            """显示学生信息"""
        print(f"姓名： {self.name}, 年龄： {self.age},成绩： {self.score}")


        def is_passing(self, passing_grade=60):
            """检查学生是否及格"""
            return self.score >= passing_grade


student1 = Student("张三",18,85)
student2 = Student("李四",19,55)
student3 = Student("王五",20,92)


student1.display_info()
print(f"{student2.name}是否及格: {student2.is_passing()}")


student_data = [
    {"name": "张三", "age": 18, "score": 85},
    {"name": "李四", "age": 19, "score": 55},
    {"name": "王五", "age": 20, "score": 92},
    {"name": "赵六", "age": 18, "score": 78},
    {"name": "钱七" ,"age": 19, "score": 63}
]


students = [Student(data["name"], data["age"], data["score"]) for data in student_data]


for i, student in enumerate(students, 1):
    print(f"学生{i}:")
    student.display_info()
    print()

class Student:
    """增强版学生类"""

    def __init__(self, name, age, score):


        if not isinstance(name, str) or len(name.strip()) == 0:
            raise ValueError("姓名必须是非空字符串")
        if not isinstance(age, int) or age <= 0:
            raise ValueError("年龄必须为正整数")
        if not isinstance(score,(int, float)) or score < 0 or score > 100:
            raise ValueError("成绩必须是0-100之间的数字")

        self.name = name.strip()
        self.age = age
        self.score = score
        self.grade = self._calculate_grade()

    def _calculate_grade(self):
        """计算等级（A/B/C/D/F）"""
        if self.score >= 90:
            return 'A'
        elif self.score >= 80:
            return'B'
        elif self.score >= 70:
            return 'C'
        elif self.score >= 60:
            return'D'
        else:
            return 'F'

    def display_info(self):
        """显示完整学生信息"""
        print(f"姓名： {self.name}")
        print(f"年龄： {self.age}岁")
        print(f"成绩： {self.score}分")
        print(f"等级： {self.grade}")
        print(f"状态： {'及格' if self.is_passing() else'不及格'}")

    def is_passing(self, passing_grade=60):
        """检查是否及格"""
        return self.score >= passing_grade

    def __str__(self):
        """定义对象的字符串表示"""
        return f"Student({self.name}, {self.age}, {self.score})"

try:
    student = Student("李明",19,88.5)
    student.display_info()
    """
    输出：
    姓名： 李明
    年龄：19岁
    成绩： 88.5分
    等级： B
    状态： 及格
    """
    print(student)
    print(repr(student))

except ValueError as e:
    print(f"错误： {e}")


class StudentManager:
    """学生管理类"""

    def __init__(self):
        self.students = []
    def add_student(self, student):
        """添加学生"""
        if isinstance(student, Student):
            self.students.append(student)
        else:
           raise TypeError("只能添加Student对象")

    def average_score(self):
        """计算平均成绩"""
        if not self.students:
            return 0
        return sum(s.score for s in self.stufents) / len(self.students)

    def top_students(self, n=3):
        """获取成绩最好的n个学生"""
        return sorted(self.students, key=lambda s: s.score, reverse=True)[:n]
    def passing_rate(self):
        """计算及格率"""
        if not self.student:
           return 0
        passing = sum(1 for s in self.students if s.is_passing())
        return passing / len(self.students) * 100
manager = StudentManager()

manager.add_student(Student("张三", 18, 85))
manager.add_student(Student("李四", 19, 55))
manager.add_student(Student("王五", 20, 92))
manager.add_student(Student("赵六", 18, 78))
manager.add_student(Student("钱七", 19, 63))

print(f"平均成绩： {manager.average_score()}:.1f")
print(f"及格率： {manager.passing_rate():.1f}%")
print("\n成绩最好的3名学生：")
for student in manager.top_student():
    print(f"- {student.name}: {student.score}分")
